// pages/particulars/particulars.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    poetName: "",
    poetObj: {},
    active: 0,
    poetryList: [],
    poetList: [],
    hotPoetImg: ['/assets/libai.png', '/assets/dufu.png', '/assets/baijuyi.png', '/assets/wangwei.png'],
  },
  async changeActive(e) {
    wx.showLoading({
      title: '加载中',
    })
    let active = e.currentTarget.dataset.index
    this.setData({
      active: active,
      poetName: this.data.poetList[active].name
    })
    this.getPoetDetail()
    this.poetPoetryList()
    wx.setStorageSync('poetName', this.data.poetName)
    // console.log(this.data.poetList[active])
  },
  /**
   * 生命周期函数--监听页面加载
   */
  jumpGO(e) {
    console.log(111);
    wx.setStorageSync('msg', e.currentTarget.dataset.msg)
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  jumpGoPoet() {
    wx.navigateTo({
      url: '../poet/poet',
    })
  },
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  getHotPoetList() {
    let that = this
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/poetry/poetHot2',
        method: "POST",
        data: {
          name: that.data.poetName
        },
        success(data) {
          data.data.rows.unshift(that.data.poetObj)
          that.setData({
            poetList: data.data.rows,
          })
          let dd = that.data.poetList.findIndex((item) => {
            return item.name == wx.getStorageSync('poetName')
          })
          console.log(dd)
          that.setData({
            active: dd,
          })
          wx.hideLoading()

          reslove({
            code: 200,
            msg: "获取成功"
          })
        }
      })
    })

  },
  getPoetDetail() {
    let that = this
    wx.request({
      url: 'http://localhost:3000/poetry/poetDetail',
      method: "POST",
      data: {
        name: that.data.poetName
      },
      success(data) {
        that.setData({
          poetObj: data.data.rows[0]
        })
      }
    })
  },
  poetPoetryList() {
    let that = this
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/poetry/poetPoetryList',
        method: "POST",
        data: {
          name: that.data.poetName
        },
        success(data) {
          data.data.rows.map((item) => {
            item.tags = JSON.parse(item.tags)
          })
          that.setData({
            poetryList: data.data.rows
          })
          reslove({
            code: 200,
            msg: "获取成功"
          })
          wx.hideLoading()
        }
      })
    })

  },
  async onShow() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      poetName: wx.getStorageSync('poetName')
    })

    this.getPoetDetail()
    this.poetPoetryList()


    await this.getHotPoetList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})